﻿using PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PublicScreenMenue
{

    public partial class FormScreenMenu : Form
    {
        private UIComponents.XPPanelGroup xpPanelGroup = new UIComponents.XPPanelGroup();
        public FormScreenMenu()
        {
            InitializeComponent();

            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.xpPanelGroup.Size = this.Size;
            this.Controls.Add(this.xpPanelGroup);

            xpPanelGroup.Anchor =
                 System.Windows.Forms.AnchorStyles.Top |
                 System.Windows.Forms.AnchorStyles.Left |
                 System.Windows.Forms.AnchorStyles.Right |
            System.Windows.Forms.AnchorStyles.Bottom;

            xpPanelGroup.Dock = DockStyle.Fill;

            InitMenu();
            // xpPanelGroup.SelectedItem += new EventHandler(sp_SubButtonClick);
        }

        private bool InitMenu()
        {
            List<ItemDataGroup> lst = ScreenMenu.MenuData().GetGroupList();
            foreach (ItemDataGroup item in lst)
            {
                if (item == null)
                    continue;

                UIComponents.XPPanel xpPanel = new UIComponents.XPPanel(36);
                xpPanel.Caption = item.Title;
                xpPanelGroup.Controls.Add(xpPanel);
                Rectangle rc = xpPanel.PanelRect;
                Point pt = rc.Location;
                System.Windows.Forms.ToolStrip toolStrip = new System.Windows.Forms.ToolStrip();
                toolStrip.LayoutStyle = ToolStripLayoutStyle.VerticalStackWithOverflow;
                toolStrip.Location = pt;

                xpPanel.Controls.Add(toolStrip);
                InitMenuChild(item, toolStrip.Items);

                int height = rc.Height;
                if (xpPanel.Controls.Count > 0)
                {
                    height = xpPanel.Controls[0].Height * xpPanel.Controls.Count;
                }

                xpPanel.Height = xpPanel.Height - xpPanel.PanelRect.Height + height;
                xpPanel.Invalidate();
            }

            return true;
        }
        private void InitMenuChild(ItemDataGroup itemGroup, ToolStripItemCollection items)
        {
            foreach (MenuItemData itemChild in itemGroup.GetChildLst())
            {
                System.Drawing.Image img = itemChild.SmallImage();

                ToolStripItem btn = null;
                if (itemChild.IsSep())
                {
                    btn = new ToolStripSeparator();
                }
                else
                {
                    btn = new ToolStripButton();
                    btn.Click += sp_SubButtonClick;
                }
                if (null == img)
                    btn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                else
                {
                    btn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
                    btn.Image = img;
                    btn.ImageAlign = ContentAlignment.BottomLeft;
                }
                btn.Alignment = ToolStripItemAlignment.Left;
                btn.Text = itemChild.Text;
                btn.Tag = itemChild.Command;
                items.Add(btn);
            }

            foreach (ItemDataGroup item in itemGroup.GetChildGroupLst())
            {
                System.Drawing.Image img = item.GetItem().SmallImage();
                ToolStripDropDownButton btnDrop = new ToolStripDropDownButton();
                if (null == img)
                    btnDrop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                else
                {
                    btnDrop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
                    btnDrop.Image = img;
                    btnDrop.ImageAlign = ContentAlignment.BottomLeft;
                }
                btnDrop.Text = item.Title;
                btnDrop.Alignment = ToolStripItemAlignment.Left;
                InitMenuChild(item, btnDrop.DropDownItems);
                items.Add(btnDrop);
                btnDrop.DropDown.Width = btnDrop.Owner.Width;
            }
        }

        public static void sp_SubButtonClick(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton btn = sender as ToolStripButton;
                if (btn != null)
                {
                    string str = "";
                    if (btn.Tag != null)
                        str = btn.Tag as string;

                    if (str != null && str.Length != 0)
                        PublicClass.CadUnits.RunLspCmd(str);
                }
            }
            catch (Exception ex)
            {
                //ex.ShowExceptionMessage();
               // Debug.WriteLine(ex.Message);
            }
        }
    }
}
